// 简单的本地代理服务器
const express = require('express')
const { createProxyMiddleware } = require('http-proxy-middleware')
const cors = require('cors')

const app = express()
const PORT = 3002

// 启用CORS
app.use(cors())

// 代理百度API
app.use('/baidu', createProxyMiddleware({
  target: 'https://aip.baidubce.com',
  changeOrigin: true,
  pathRewrite: {
    '^/baidu': ''
  },
  onProxyReq: (proxyReq, req, res) => {
    console.log('代理请求:', req.method, req.url)
  },
  onProxyRes: (proxyRes, req, res) => {
    console.log('代理响应:', proxyRes.statusCode)
  }
}))

app.listen(PORT, () => {
  console.log(`代理服务器运行在 http://localhost:${PORT}`)
  console.log('使用方法: http://localhost:3002/baidu/oauth/2.0/token')
})
